<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 幕小铺科技（大连）有限公司 [ http://www.XXX.com ]
// +----------------------------------------------------------------------
// | 2020/03/14 jason wang
// +----------------------------------------------------------------------
namespace app\user_center\logic\mcn;
use think\facade\Hook;
use think\DB;

class Mcnlogic{

	/**
     * Mcn-艺人列表
     * @param  int $uid  			用户ID
     * @param  int $attestation_id  机构ID
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
    public function artist_list($uid){
        try{
            $params = [
                'uid'   	=> $uid,
            ];
			
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            };
            /********前置行为********/

            /********业务逻辑********/
                $attestation_id = model('UcAttestation', 'model\mcn') -> getDataByUid($uid);
				if($attestation_id['code'] < 0){
					DB::rollback();
					return array_out(-1005, 'error1');//逻辑错误
				}
                // 数据列表
				$attestation_id = $attestation_id['data'] -> id;
                $map = [];
                $map[] = ['attestation_id', '=', $attestation_id];
                $data_list = model('UcAttestationArtist', 'model\mcn') -> where($map)  -> order('create_time desc') -> paginate();
				if(empty($data_list)){
					DB::rollback();
					return array_out(-1005, 'error2');//逻辑错误
				}
				
                foreach ($data_list as $key => $value) {
                    $result_list[] = [
                        'artistId'      => (int)$value -> id,
                        'artistName'    => (string)$value -> artist_name,
                        'artistStatus'  => (int)$value -> artist_status,
                        'fansTimes'  	=> (int)$value -> get_uc_member_count -> fans_times,
                        'worksTimes'  	=> (int)$value -> get_uc_member_count -> works_times,
                        'followTimes'  	=> (int)$value -> get_uc_member_count -> follow_times,
                        'bePraisedTimes'=> (int)$value -> get_uc_member_count -> be_praised_times,
                        'praisedTimes'  => (int)$value -> get_uc_member_count -> praised_times,
                        'hotNum'  		=> (int)$value -> get_uc_member_count -> hot_num,
                        'cTime'         => date('Y-m-d H:i:s', $value -> create_time),
                    ];
                }
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			
            return array_out(200, 'success', $result_list);
        } catch (\Exception $e) {
            return array_out(-9999, $e -> getMessage());
        }
    	
    }
	
	/**
     * Mcn-添加艺人
     * @param  int 		$attestation_id  机构ID
     * @param  int 		$artist_mobile   艺人手机
     * @param  string 	$artist_name  	 艺人名称
     * @param  int 		$artist_status   状态 0禁用  1启用
     * @param  int 		$classid   分类id
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
    public function add_artist($user_id, $artist_mobile, $artist_name, $artist_status, $classid = ''){
        DB::startTrans();//启动事务
        try{
            $params = [
                'user_id'   	    => $user_id,
                'artist_mobile'   	=> $artist_mobile,
                'artist_name'   	=> $artist_name,
                'artist_status'   	=> $artist_status,
                'classid'   	    => $classid,
            ];
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
				DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            };
            /********前置行为********/
            
            /********业务逻辑********/
                //查询用户uid
                $res1 = model('UcMemberMobile', 'model\user') -> getDataByMobile($artist_mobile);
				if($res1['code'] < 0){
					DB::rollback();
					return array_out(-1005, '该用户还不是会员，请先注册会员后再试');//逻辑错误
				}
                $uid = $res1['data'] -> uid;
                //添加机构数据
                $attestation_id = model('UcAttestation', 'model\mcn') -> getDataByUid($user_id);//新增得机构是否存在 不应该是uid
				if($attestation_id['code'] < 0){
					DB::rollback();
					return array_out(-1005, 'error2');//逻辑错误
				}
				
				$attestation_id = $attestation_id['data'] -> id;
                $res = model('UcAttestationArtist', 'model\mcn') -> addData($uid, $attestation_id, $artist_name, $artist_status, $classid);
				if($res < 0){
					DB::rollback();
					return array_out(-1005, 'error3');//逻辑错误
				}
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
				DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
			DB::commit();
			return array_out(200, 'success');
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
    	
    }
	
	/**
     * Mcn-更新艺人信息
     * @param  int 		$uid  			 用户ID
     * @param  int 		$attestation_id  机构ID
     * @param  int 		$artist_id  	 艺人ID
     * @param  int 		$artist_mobile   艺人手机
     * @param  string 	$artist_name  	 艺人名称
     * @param  int 		$artist_status   状态 0禁用  1启用
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
    public function edit_artist($uid, $artist_id, $artist_mobile, $artist_name, $artist_status){
        DB::startTrans();//启动事务
        try{
            $params = [
                'uid'		 		=> $uid,
                'artist_id'   		=> $artist_id,
                'artist_mobile'   	=> $artist_mobile,
                'artist_name'   	=> $artist_name,
                'artist_status'   	=> $artist_status,
            ];
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
				DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            };
            /********前置行为********/

            /********业务逻辑********/
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
				DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
            return array_out(200, 'success');
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, '未知错误');
        }
    }
	
	/**
     * Mcn-删除艺人信息
     * @param  int 		$uid  			 用户ID
     * @param  int 		$artist_id  	 艺人ID
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
    public function delete_artist($uid, $artist_id){
        DB::startTrans();//启动事务
        try{
            $params = [
                'uid'		 		=> $uid,
                'artist_id'   		=> $artist_id,
            ];
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
				DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            };
            /********前置行为********/

            /********业务逻辑********/
                //删除数据
                $res = model('UcAttestationArtist', 'model\mcn') -> destroy($artist_id);
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
				DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
            if($res){
                DB::commit();
                return array_out(200, 'success');
            }else{
                DB::rollback();
                return array_out(-1005, 'error');
            }
        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());
        }
    }
	
	
	/**
     * Mcn-艺人详情
     * @param  int 		$uid  			 用户ID
     * @param  int 		$attestation_id  机构ID
     * @param  int 		$artist_id  	 艺人ID
     * @author shiqiang <976162041@qq.com>
     * @return array_out
     */
    public function get_artist_info($uid, $attestation_id, $artist_id){
        try{
            $params = [
                'uid'		 		=> $uid,
                'attestation_id'   	=> $attestation_id,
                'artist_id'   		=> $artist_id,
            ];
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            };
            /********前置行为********/

            /********业务逻辑********/
            /********业务逻辑********/

           /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/
            return array_out(200, 'success');
        } catch (\Exception $e) {
            return array_out(-9999, $e -> getMessage());
        }
    }
}